<?php
/**
 * This class is for create,read,update,delete of users
 * @author Raymond 2015.02.11
 */
class UserController extends BaseController {

	// Table
	public $table = 'user';

	// Eloquent Model
	public $model = 'User';

	// Base Route
	public $base = 'user';

	// Localization
	public $local = 'en';

	// Language Name
	public $language = 'user';

	// Breadcrumbs
	public $breadcrumbs = [ ['link' => 'user' , 'title' => 'User'] ];

	// Set List Data - Input, Object
	public function setListData( $data, $object ) {

		// Show All the list except ADMIN ( Super User )
		if( Auth::user()->username != Constant::SUPER_USER )
			$object = $object->where('username', '<>', Constant::SUPER_USER);
			
		// Filter the user 
		if( !empty($data['filter_id']) ) {
			$object = $object->where('id', '=', $data['filter_id']);
		}
		
		// Filter by user group id
		if( !empty($data['filter_email']) ) {
			$object = $object->where('email', 'LIKE', '%' . $data['filter_email'] . '%');
		}
		
		// Filter by username
		if( !empty($data['filter_username']) ) {
			$object = $object->where('username', 'LIKE', '%' . $data['filter_username'] . '%');
		}

		return $object;
	}

	// Process before storing data
	public function preStore( $data ) {
		
		// Return validated data
		$this->_validate( $data );
		$this->rules['password'] = 'required|confirmed|min:8|alpha_num';
		$this->rules['password_confirmation'] = 'required|min:8|alpha_num|same:password';
		
		if( !preg_match("/^(?=.*\d)[0-9a-zA-Z]{0,}$/", $data['password'] ) ){
			$this->error['password'][] = Lang::get($this->language.'.error_password_digits' );
			$this->error['password_confirmation'][] = Lang::get($this->language.'.error_password_digits' );
		}
		if(!preg_match("/^(?=.*[A-Z])[0-9a-zA-Z]{0,}$/", $data['password'] ) ){
			$this->error['password'][] = Lang::get($this->language.'.error_password_uppercase');
			$this->error['password_confirmation'][] = Lang::get($this->language.'.error_password_uppercase');
		}

		return $data;
	}
	
	// Use to manipulate data after 'store' or 'inserting'
	public function postStore( $data, $object ) {
	
		// Save the user information from the form.
		$object->image = $data['user_image'];
		$object->password = Hash::make($data['password']);
		$object->signature_image = $data['signature_image'];
		$object->save();
		
		return $object;
	}
	
	// Process before updating data
	public function preUpdate( $data ) {

		// Return validated data
		$this->_validate( $data );
		
		// Required to input password when passwor confirmed is not empty
		if( !empty($data['password_confirmation'] )  || !empty($data['password'])){
			$this->rules['password']='min:8|alpha_num';
			$this->rules['password_confirmation'] = 'min:8|alpha_num|same:password';
			
			if( !preg_match("/^(?=.*\d)[0-9a-zA-Z]{0,}$/", $data['password'] ) ){
				$this->error['password'][] = Lang::get($this->language.'.error_password_digits' );
				$this->error['password_confirmation'][] = Lang::get($this->language.'.error_password_digits' );
			}
			if(!preg_match("/^(?=.*[A-Z])[0-9a-zA-Z]{0,}$/", $data['password'] ) ){
				$this->error['password'][] = Lang::get($this->language.'.error_password_uppercase');
				$this->error['password_confirmation'][] = Lang::get($this->language.'.error_password_uppercase');
			}
		}
			
		return $data;
	}
	
	// Use to manipulate data after 'store' or 'inserting'
	public function postUpdate( $data, $object ) {
		
		// Save the user information from the form.
		if( empty($data['password']) )
			unset($data['password']);
		else
			$object->password = Hash::make($data['password']);
		
		// Save the user information from the form.
		$object->image = $data['user_image'];
		$object->password = Hash::make($data['password']);
		$object->signature_image = $data['signature_image'];
		$object->save();
		
		$object->save();
		return $data;
	}
	
	/**
	 * Use to validate requests
	 * @author Raymond 2/11/2015 10:25pm
	 * @param $data
	 * @return mixed
	 */
	private function _validate( $data ) {

		// Updated At Rules
		$this->rules = array(
			'email' => 'required|email',
			'username' => 'required',
			'lastname' => 'required',
			'firstname' => 'required'
		);
		// trime the lastname and firstname
		$data['lastname'] = trim($data['lastname']);
		$data['firstname'] = trim($data['firstname']);
		
		// Has spaces and special characters? invalid
		if ( preg_match("/\s\s/", $data['lastname'] ) ||
			!preg_match("/^[A-Za-z\s\-]+$/", $data['lastname']) ) {
			$this->error['lastname'][] = Lang::get($this->language.'.error_lastname');
		}

		if ( preg_match("/\s\s/", $data['firstname'] ) ||
			!preg_match("/^[A-Za-z\s\-]+$/", $data['firstname']) ) {
			$this->error['firstname'][] = Lang::get($this->language.'.error_firstname');
		}
		
	}
	
	/**
	 * Used in uploading user image
	 * @author Geoffrey
	 * @param none
	 * @return none
	 */
	public function uploadUserImage($id){
		$user_image = Input::file('uploadUserImage');
		$moved = $user_image->move(Config::get('app.lbea'), Input::file('uploadUserImage')->getClientOriginalName());
		
		$user = User::find($id);
		$user->image = Input::file('uploadUserImage')->getClientOriginalName();
		$user->save();
		
		return Response::json([ 'field_target' => 'user_image', 'image_link' => Asset::bea('image/uploads/' . Input::file('uploadUserImage')->getClientOriginalName()), 'image' => Input::file('uploadUserImage')->getClientOriginalName() ]);
	}
	
	/**
	 * Used in uploading a new image when creating a user
	 * @author Geoff
	 * @param none
	 * @return number
	 */
	public function uploadNewUserImage(){
		$user_image = Input::file('uploadNewUserImage');
		$moved = $user_image->move(Config::get('app.lbea'), Input::file('uploadNewUserImage')->getClientOriginalName());
		
		return Response::json([ 'field_target' => 'user_image', 'image_link' => Asset::bea('image/uploads/' . Input::file('uploadNewUserImage')->getClientOriginalName()), 'image' => Input::file('uploadNewUserImage')->getClientOriginalName() ]);
	}
	
	/**
	 * Used in upload a new image signature when creating a user
	 * @author Geoff
	 * @param none
	 * @return json
	 */
	public function uploadNewUserSignatureImage(){
		$user_image = Input::file('uploadNewUserSignatureImage');
		$moved = $user_image->move(Config::get('app.lbea'), Input::file('uploadNewUserSignatureImage')->getClientOriginalName());
		
		return Response::json([ 'field_target' => 'signature_image', 'image_link' => Asset::bea('image/uploads/' . Input::file('uploadNewUserSignatureImage')->getClientOriginalName()), 'image' => Input::file('uploadNewUserSignatureImage')->getClientOriginalName() ]);
	}
	
	/**
	 * Used in uploading a new user signature when editing a user
	 * @author Geoff
	 * @param $id
	 * @return json
	 */
	public function uploadUserSignatureImage($id){
		$user_image = Input::file('uploadUserSignatureImage');
		$moved = $user_image->move(Config::get('app.lbea'), Input::file('uploadUserSignatureImage')->getClientOriginalName());
		
		$user = User::find($id);
		$user->image = Input::file('uploadUserSignatureImage')->getClientOriginalName();
		$user->save();
		
		return Response::json([ 'field_target' => 'signature_image', 'image_link' => Asset::bea('image/uploads/' . Input::file('uploadUserSignatureImage')->getClientOriginalName()), 'image' => Input::file('uploadUserSignatureImage')->getClientOriginalName() ]);
	}
}
